package Comprehensive_04;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test8 {
    private static final ArrayList<String> skillsRole1 = new ArrayList<>();
    private static final ArrayList<String> skillsRole2 = new ArrayList<>();
    private static final Lock lock = new ReentrantLock();
    private static final Condition conA = lock.newCondition();
    private static final Condition conB = lock.newCondition();

    static {
        Collections.addAll(skillsRole1, "铁山靠", "佛山只因角", "黑只因掏心");
        Collections.addAll(skillsRole2, "肉蛋葱只因", "回首掏,唷~鬼刀一开看不见,走胃走胃,手里干", "天降神龙");
    }

    private static final Random rand = new Random();

    public static void main(String[] args) {
        Role role1 = new Role("菜虚鲲", skillsRole1);
        Role role2 = new Role("芜湖大司马", skillsRole2);
        Object o = new Object();
        new Thread(() -> {
            while (true) {
                try {
                    lock.lock();
                    getWinner(role1, role2);
                    int attackBleed = rand.nextInt(10);
                    int getSkill = rand.nextInt(role1.getSkills().size());
                    role2.setBLEED(role2.getBLEED() - attackBleed);
                    System.out.println(role1.getName() + "对" + role2.getName() + "使出了一招," + role1.getSkills().get(getSkill) + ",造成了" + attackBleed + "点伤害");
                    conA.signal();
                    conB.await();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }

        }).start();
        new Thread(() -> {
            while (true) {
                try {
                    lock.lock();
                    getWinner(role1, role2);
                    int attackBleed = rand.nextInt(10);
                    int getSkill = rand.nextInt(role2.getSkills().size());
                    role1.setBLEED(role1.getBLEED() - attackBleed);
                    System.out.println(role2.getName() + "对" + role1.getName() + "使出了一招" + role2.getSkills().get(getSkill) + ",造成了" + attackBleed + "点伤害");
                    conB.signal();
                    conA.await();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }).start();
    }

    private static void getWinner(Role role1, Role role2) {
        if (role1.getBLEED() <= 0) {
            System.out.println(role2.getName() + "打败了" + role1.getName());
            System.exit(0);
        } else if (role2.getBLEED() <= 0) {
            System.out.println(role1.getName() + "打败了" + role2.getName());
            System.exit(0);
        }
    }

}

class Role {
    private String name;
    private int BLEED = 100;

    public int getBLEED() {
        return BLEED;
    }

    public void setBLEED(int BLEED) {
        this.BLEED = BLEED;
    }

    private ArrayList<String> skills;

    public Role() {
    }

    public Role(String name, ArrayList<String> skills) {
        this.name = name;
        this.skills = skills;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return skills
     */
    public ArrayList<String> getSkills() {
        return skills;
    }

    /**
     * 设置
     *
     * @param skills
     */
    public void setSkills(ArrayList<String> skills) {
        this.skills = skills;
    }

    public String toString() {
        return "Role{name = " + name + ", BLEED = " + BLEED + ", skills = " + skills + "}";
    }
}
